package com.fjd.controller;

import com.fjd.dto.AfterSaleDTO;
import com.fjd.entity.AfterSale;
import com.fjd.result.Result;
import com.fjd.service.AfterSaleService;
import com.fjd.service.OrderService;
import com.fjd.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shopping/aftersale")
@Slf4j
@Api(tags = "售后管理")
public class AfterSaleController {
    @Autowired
    private AfterSaleService afterSaleService;
    @Autowired
    private OrderService orderService;
    @GetMapping("/list")
    @ApiOperation("查询所有售后服务")
    public Result<List<AfterSale>> list(){
        List<AfterSale> list=afterSaleService.list();
        return Result.success(list);
    }

    @GetMapping("/search")
    @ApiOperation("查询某一售后服务")
    public Result<Map<String, Object>> getById(String id){
        AfterSale afterSale = afterSaleService.findById(id);
        OrderVO orderVO=orderService.select(afterSale.getOrderId());
        List<String> image=afterSaleService.getImage(id);
        Map<String, Object> result = new HashMap<>();
        result.put("aftersale", afterSale);
        result.put("order",orderVO);
        result.put("image",image);
        return Result.success(result);
    }

    @PostMapping("/add")
    @ApiOperation("添加售后服务")
    public Result add(@RequestBody AfterSaleDTO afterSaleDTO){
        afterSaleService.insertAfterSale(afterSaleDTO);
        return Result.success();
    }

    @GetMapping("/update")
    @ApiOperation("修改售后状态")
    public Result update(String id,String state,String note){
        afterSaleService.updateAfterSale(id,state,note);
        return Result.success();
    }

    @GetMapping("/delete")
    @ApiOperation("取消售后申请")
    public Result delete(String id){
        afterSaleService.deleteAfterSale(id);
        System.out.println(id);
        return Result.success();
    }
}
